package com.smarthome.core.payment.alipay.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.smarthome.common.tools.MoneyUtils;
import com.smarthome.core.payment.alipay.AlipayService;
import com.smarthome.core.payment.alipay.util.AlipayConfig;
import com.smarthome.core.payment.alipay.util.AlipayNotify;
import com.smarthome.core.payment.alipay.util.AlipayReqUtil;
import com.smarthome.core.payment.alipay.util.BankCallbackDomain;
import com.smarthome.core.payment.alipay.util.BankRes;
import com.smarthome.core.propertymanager.PropertyFeePaymentService;
import com.smarthome.domain.enums.AppIdEnum;

@Service("alipayService")
public class AlipayServiceImpl implements AlipayService {

    public static final Logger LOGGER = LoggerFactory.getLogger(AlipayService.class);
    @Resource
    private PropertyFeePaymentService propertyFeePaymentService;

    @Override
    public String payByAlipayMobile(String appId, String orderId, long amount, String goodsName) {
        Map<String, String> sParaTemp = new HashMap<String, String>();
        sParaTemp.put("service", "mobile.securitypay.pay");
        sParaTemp.put("partner", AlipayConfig.partner);
        sParaTemp.put("_input_charset", AlipayConfig.input_charset);
        sParaTemp.put("notify_url", AlipayConfig.notifyUrl);
        sParaTemp.put("out_trade_no", orderId);
        sParaTemp.put("subject", goodsName);
        sParaTemp.put("payment_type", "1");// 商品购买
        sParaTemp.put("seller_id", AlipayConfig.sellerId);
        sParaTemp.put("total_fee", String.valueOf(MoneyUtils.fenToYuan(amount)));
        sParaTemp.put("body", goodsName);
        sParaTemp.put("app_id", appId);
        return AlipayReqUtil.generateAlipayMobileReqStr(sParaTemp);
    }

    @Override
    public void doPayByAlipay(BankRes bankRes) {
        Map<String, String> params = bankRes.getParams();
        String trade_status = params.get("trade_status");
        // 获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
        if (AlipayNotify.verify(params) && (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS"))) {// 验证成功
            String outTradeno = params.get("out_trade_no");
            String outTradenoArr[] = outTradeno.split("_");
            BankCallbackDomain bankCallbackDomain = new BankCallbackDomain();
            bankCallbackDomain.setAmount(MoneyUtils.yuanToFen(new BigDecimal(params.get("total_fee"))));
            bankCallbackDomain.setOrderId(outTradeno);
            bankCallbackDomain.setTranSerialNo(params.get("trade_no"));
            bankCallbackDomain.setTranStat(1);
            bankCallbackDomain.setParameters(bankRes.getNotifyData());
            bankCallbackDomain.setAppIdObj(AppIdEnum.getByCode(outTradenoArr[0]));
            propertyFeePaymentService.finishPayment(bankCallbackDomain);
        } else {
            LOGGER.error("alipay-payment-notify:" + JSONObject.toJSONString(params));
        }
    }

}
